<template>
	<div class="detail_main">
		<div class="detail_main_top">
			<div class="detail_top_left">
				<el-button type="primary" @click="backUrl" size='small' class='detail_top_leftP1'>返回</el-button>
				<div class="detail_top_leftP2">患者：{{ caseinfo.patientName }} {{ caseinfo.patientSex == 'F' ? '女' : '男'
				}}
					{{ caseinfo.patientAge }}岁</div>
			</div>
			<span>{{ caseinfo.caseName }}（病例详情）</span>
			<div class="detail_top_right">
				<el-button type='primary' size='small' @click='toCase'>查看影像</el-button>
			</div>
		</div>
		<div class="detail_main_bottom clearfix">
			<mindmap :isAll='isAll' :mindMap="caseinfo.mindMap" v-if='caseinfo.caseId'></mindmap>
			<detailright @closeAll="isAll = ''" :isAll='isAll' :caseinfo="caseinfo" v-if='caseinfo.caseId'>
			</detailright>
			<div class="detail_main_tabs">
				<div class="detail_main_tab" :class="isAll == '1' ? 'active' : ''" @click="openIsAll(1)"
					v-if="caseinfo.application">
					检 查 申 请 单
				</div>
				<div class="detail_main_tab" :class="isAll == '2' ? 'active' : ''" @click="openIsAll(2)"
					v-if="caseinfo.hospitalCase">
					住 院 病 例
				</div>
				<div class="detail_main_tab" @click="showcase = !showcase" v-if="reports && reports.length">
					影 像 报 告
				</div>
				<div class="detail_main_tab" :class="isAll == '3' ? 'active' : ''" @click="openIsAll(3)"
					v-if="caseinfo.questionJson">
					练 习 题
				</div>
				<div class="detail_main_tab" @click="openDio('1')" v-if="caseinfo.knowledgeGraph">
					知 识 图 谱
				</div>
				<div class="detail_main_tab" @click="openDio('2')" v-if="caseinfo.knowledgePoint">
					知 识 点
				</div>
			</div>
		</div>
		<div class="index_mask" v-show="showcase">
			<div class="csae_dio_contbox">
				<div class="csae_dio_contitem">
					<div class="csae_dio_title">
						正确报告 - 诊断报告单
					</div>
					<div class="csae_dio_main beautifulScroll">
						<el-row class='csae_dio_mainP1'>
							<el-col :span='6'>
								<div class="csae_dio_mainP2">
									<b>姓名：</b>{{ caseinfo.patientName }}
								</div>
							</el-col>
							<el-col :span='6'>
								<div class="csae_dio_mainP2">
									<b>年龄：</b>{{ caseinfo.patientAge }}岁
								</div>
							</el-col>
							<el-col :span='12'>
								<div class="csae_dio_mainP2">
									<b>性别：</b>{{ caseinfo.patientSex == "F" ? '女' : '男' }}
								</div>
							</el-col>
							<el-col :span='6'>
								<div class="csae_dio_mainP2">
									<b>病历号：</b>{{ caseinfo.number }}
								</div>
							</el-col>
							<el-col :span='6'>
								<div class="csae_dio_mainP2">
									<b>检查类型：</b><span v-for='item in modalitys'
										v-if='caseinfo.modalityId == item.modalityId'>{{ item.modalityName }}</span>
								</div>
							</el-col>
							<el-col :span='12'>
								<div class="csae_dio_mainP2">
									<b>检查时间：</b>{{ caseinfo.studyTime }}
								</div>
							</el-col>
						</el-row>
						<el-row class='csae_dio_mainP1'>
							<el-col :span='24'>
								<div class="csae_dio_mainP2">
									<b>检查部位：</b>{{ caseinfo.bodyName }}
								</div>
							</el-col>
						</el-row>
						<el-row class='csae_dio_mainP1 bottom'>
							<el-col :span='24'>
								<div class="csae_dio_mainP2">
									<b>影像表现：</b>
								</div>
								<div class="csae_dio_mainP3">
									{{ caseinfo.find }}
								</div>
							</el-col>
							<el-col :span='24'>
								<div class="csae_dio_mainP2">
									<b>检查结论：</b>
								</div>
								<div class="csae_dio_mainP3">
									{{ caseinfo.diagnosis }}
								</div>
							</el-col>
						</el-row>
					</div>
				</div>
				<div class="csae_dio_contitem">
					<div class="csae_dio_title">
						我的-诊断报告单
					</div>
					<div class="csae_dio_close like_btn" @click="showcase = false">
						<i class="el-icon-close"></i>
					</div>
					<div class="csae_dio_contcontrols">
						<div class="csae_dio_contcontrol " @click="changeNum(-1)"
							:class="reportIndex <= 0 ? 'disabled' : ''">
							<i class="el-icon-arrow-left"></i>
						</div>
						<div class="l csae_dio_contcontrol_num">
							{{ reportIndex + 1 }}/{{ reports.length }}
						</div>
						<div class="csae_dio_contcontrol " @click="changeNum(1)"
							:class="reportIndex >= reports.length - 1 ? 'disabled' : ''">
							<i class="el-icon-arrow-right"></i>
						</div>
					</div>
					<emptymain :emptytext="'暂无诊断报告'" :emptyheight="'550px'" :emptylist='reports'>
						<span></span>
					</emptymain>
					<div class="csae_dio_main noborder beautifulScroll" v-if="reportIndex > -1">
						<div class="csae_dio_item">
							<el-row class='csae_dio_mainP1'>
								<el-col :span='6'>
									<div class="csae_dio_mainP2">
										<b>姓名：</b>{{ caseinfo.patientName }}
									</div>
								</el-col>
								<el-col :span='6'>
									<div class="csae_dio_mainP2">
										<b>年龄：</b>{{ caseinfo.patientAge }}岁
									</div>
								</el-col>
								<el-col :span='12'>
									<div class="csae_dio_mainP2">
										<b>性别：</b>{{ caseinfo.patientSex == "F" ? '女' : '男' }}
									</div>
								</el-col>
								<el-col :span='6'>
									<div class="csae_dio_mainP2">
										<b>病历号：</b>{{ caseinfo.number }}
									</div>
								</el-col>
								<el-col :span='6'>
									<div class="csae_dio_mainP2">
										<b>检查类型：</b><span v-for='item in modalitys'
											v-if='caseinfo.modalityId == item.modalityId'>{{ item.modalityName }}</span>
									</div>
								</el-col>
								<el-col :span='12'>
									<div class="csae_dio_mainP2">
										<b>提交时间：</b>{{ reports[reportIndex].createTime }}
									</div>
								</el-col>
								<el-col :span='12' v-if='reports[reportIndex].evaluateTime'>
									<div class="csae_dio_mainP2">
										<b>评分时间：</b>{{ reports[reportIndex].evaluateTime }}
									</div>
								</el-col>
							</el-row>
							<el-row class='csae_dio_mainP1'>
								<el-col :span='24'>
									<div class="csae_dio_mainP2">
										<b>检查部位：</b>{{ caseinfo.bodyName }}
									</div>
								</el-col>
							</el-row>
							<el-row class='csae_dio_mainP1 bottom'>
								<el-col :span='24'>
									<div class="csae_dio_mainP2">
										<b>影像表现：</b>
									</div>
									<div class="csae_dio_mainP3">
										{{ reports[reportIndex].find }}
									</div>
								</el-col>
								<el-col :span='24'>
									<div class="csae_dio_mainP2">
										<b>检查结论：</b>
									</div>
									<div class="csae_dio_mainP3">
										{{ reports[reportIndex].diagnosis }}
									</div>
								</el-col>
							</el-row>
						</div>
						<div class="csae_dio_teacher" v-if="reports[reportIndex].score">
							<div class="csae_dio_teacherP1">评分评语</div>
							<div class="csae_dio_teacherP2">
								<el-rate v-model="reports[reportIndex].score / 2" :colors="colors" show-text
									:texts='texts' disabled>
								</el-rate>
							</div>
							<div class="csae_dio_teacherP2">
								<div class="csae_dio_mainP2 big">
									<b>教师评语：</b>
								</div>
								<div class="csae_dio_mainP3 big">
									<span class="editor_box" v-html="reports[reportIndex].evaluate"></span>
								</div>
							</div>
						</div>
					</div>
					<div class="csae_dio_bottom" v-if="reportIndex > -1">
						<el-button type='success' :loading="aiLoading"
							@click='aiItemReport(reports[reportIndex])'>报告分析</el-button>
						<el-button type='danger' @click='deleteItemReport(reports[reportIndex])'>删除报告</el-button>
					</div>
				</div>
			</div>
		</div>
		<reportAnalysismain :reportAnalysis="reports[reportIndex].reportAnalysis" :infoForm="caseinfo"
			:reportDataInfo="reports[reportIndex]" :modalitys="modalitys"
			v-if="reportAnalysisShow" @closeOpen="reportAnalysisShow = false">
		</reportAnalysismain>
		<div class="index_mask" v-show="showdio">
			<div class="ai_dio">
				<img src="@/images/ai_dioback.png" class="ai_dioback">
				<div class="ai_close like_btn" @click="showdio = false"><i class="el-icon-close"></i></div>
				<div class="ai_tabs">
					<div class="ai_tab" v-if="activeTab == 1">
						<i class="el-icon-s-marketing"></i>知识图谱
					</div>
					<div class="ai_tab" v-if="activeTab == 2">
						<i class="el-icon-s-opportunity"></i>知识点
					</div>
				</div>
				<div class="ai_content" v-show="activeTab == 1">
					<div class="ai_content2 all">
						<knowledge :graphData="knowledgeGraph" v-if="showdio && activeTab == 1"></knowledge>
					</div>
				</div>
				<div class="ai_content" v-show="activeTab == 2">
					<div class="ai_content2 all">
						<knowledgepoint :knowledgePoint="knowledgePoints" v-if="showdio && activeTab == 2">
						</knowledgepoint>
					</div>
				</div>
				<div class="csae_dio_bottom">
					<el-button type='primary' @click='showdio = false'>确定</el-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import { caseInfo, saveReport, reportList, deleteReport, aiReportAnalysis } from '@/api/case.js'
import { modalityList } from '@/api/image.js'
import mindmap from './mindmap.vue'
import detailright from './detailright.vue'
import knowledge from './knowledge.vue'
import knowledgepoint from './knowledgepoint.vue'
import reportAnalysismain from './reportAnalysis.vue'
export default {
	components: {
		mindmap,
		detailright,
		knowledge,
		knowledgepoint,
		reportAnalysismain
	},
	data() {
		return {
			caseId: this.$route.query.caseId,
			isAll: '',
			colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
			texts: ['2分  失望', '4分  不满', '6分  一般', '8分  满意', '10分  惊喜'],
			showcase: false,
			aiLoading: false,
			reportAnalysisShow:false,
			knowledgePoint: [],
			modalitys: [],
			medicalHistory0: {
				label: '既往史',
				value: ''
			},
			medicalHistory1: {
				label: '家族史',
				value: ''
			},
			medicalHistory2: {
				label: '婚育史',
				value: ''
			},
			medicalHistory3: {
				label: '现病史',
				value: ''
			},
			medicalHistory4: {
				label: '个人史',
				value: ''
			},
			caseinfo: {},

			reports: [],
			reportIndex: -1,

			fromname: '',

			showdio: false,
			activeTab: 1,
			knowledgeGraph: {},
			knowledgePoints: [],

		}
	},
	created() {
		this.getDataInfo()
		this.getModalityList()
		this.getDataList()
	},
	mounted() { },
	beforeRouteEnter(to, from, next) {
		next(that => {
			that.fromname = from.name
		})
	},
	methods: {
		markdownConverted(content) {
			if (!content) return '';
			return marked.parse(content);
		},
		openDio(activeTab) {
			this.showdio = true
			this.activeTab = activeTab
		},
		openIsAll(isAll) {
			if (this.isAll != isAll) {
				this.isAll = isAll + ''
			} else {
				this.isAll = ''
			}
		},
		backUrl() {
			if (this.fromname == 'caseimage') {
				window.close()
			} else {
				this.$router.back(-1)
			}
		},
		changeNum(num) {
			if (num < 0 && this.reportIndex > 0) {
				this.reportIndex--
			}
			if (num > 0 && this.reportIndex < this.reports.length - 1) {
				this.reportIndex++
			}
		},
		toCase() {
			const routeUrl = this.$router.resolve({
				name: 'caseimage',
				query: {
					caseId: this.caseId,
				}
			})
			window.open(routeUrl.href, '_blank')
		},
		getModalityList() {
			modalityList({}).then(res => {
				this.modalitys = res.data || [];
			})
		},
		getDataInfo() {
			caseInfo({
				caseId: this.caseId,
			}).then(res => {
				this.caseinfo = res.data || {};
				if (this.caseinfo.medicalHistory) {
					var medicalHistory = JSON.parse(this.caseinfo.medicalHistory);
					medicalHistory.map(item => {
						this[item.name] = item
					})
				}
				try {
					this.knowledgeGraph = this.caseinfo.knowledgeGraph ? JSON.parse(this.caseinfo.knowledgeGraph) : []
					this.knowledgePoints = this.caseinfo.knowledgePoint ? JSON.parse(this.caseinfo.knowledgePoint) : []
				} catch (error) {

				}
			})
		},
		aiItemReport(item) {
			if (this.reports[this.reportIndex].reportAnalysis) {
				this.reportAnalysisShow = true
				return
			}
			var patientSex = this.caseinfo.patientSex == "F" ? '女' : '男';
			var patientCase = '性别：' + patientSex + '，';
			patientCase += '年龄：' + this.caseinfo.patientAge + '岁;';
			var list = [0, 1, 2, 3, 4]
			list.map(item => {
				patientCase += this['medicalHistory' + item].label + ':' + this['medicalHistory' + item].value + '；'
			})
			var data = {
				patientCase,
				systemReport: '影像所见：' + this.caseinfo.find + '；影像结论：' + this.caseinfo.diagnosis,
				studentReport: '影像所见：' + item.find + '；影像结论：' + item.diagnosis,
			}
			this.aiLoading = true
			aiReportAnalysis(data).then(res => {
				if (res.code == 200) {
					this.reports[this.reportIndex].reportAnalysis = res.data
					this.reportAnalysisShow = true
				}
			}).finally(() => {
				this.aiLoading = false
			})
		},
		deleteItemReport(item) {
			this.$confirm('确认删除此诊断报告吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				deleteReport({
					reportId: item.reportId,
				}).then(res => {
					if (res.code == 200) {
						this.getDataList()
					} else {
						this.$message({
							type: 'error',
							message: res.message
						})
					}
				})
			}).catch(() => { })
		},
		getDataList() {
			reportList({
				caseId: this.caseId,
			}).then(res => {
				this.reports = res.data || [];
				if (this.reports && this.reports.length) {
					this.reportIndex = 0
				} else {
					this.reportIndex = -1
				}
			})
		},
	},
}
</script>